import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {
    public static void main(String[] args) throws Exception {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        reflectPrivateField();
        //reflectPrivateMethod();
    }

    /**
     * 反射私有方法
     */
    private static void reflectPrivateMethod() throws Exception {
        Class<?> classStudent = Class.forName("Student");
        Method methodStudent = classStudent.getDeclaredMethod("function",String.class);
        System.out.println("私有方法的方法名为："+methodStudent.getName());
        methodStudent.setAccessible(true);
        Object objectStudent = classStudent.newInstance();
        Student student = (Student) objectStudent;
        methodStudent.invoke(student,"我是给私有的function函数传的参数");
    }

    /**
     * 反射私有属性
     */
    private static void reflectPrivateField() {
        try {
            Class<?> classStudent = Class.forName("Student");
            Field field = classStudent.getDeclaredField("name");
            field.setAccessible(true);
            Object newInstance = classStudent.newInstance();
            Student student = (Student) newInstance;
            field.set(student,"李四");
            String name = (String) field.get(student);
            System.out.println("反射私有属性修改了name："+ name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射私有构造方法
     */
    private static void reflectPrivateConstructor() {
        try {
            Class<?> classStudent = Class.forName("Student");
            Constructor<?> declaredConstructor =
                    classStudent.getDeclaredConstructor(String.class, int.class);
            declaredConstructor.setAccessible(true);
            Object zhangsan = declaredConstructor.newInstance("zhangsan", 17);
            Student student = (Student) zhangsan;
            System.out.println("获得私有构造哈数且修改姓名和年龄"+student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建对象
     */
    private static void reflectNewInstance() {
        try {
            Class<?> classStudent = Class.forName("Student");
            Object newInstance = classStudent.newInstance();
            Student student = (Student) newInstance;
            System.out.println("获得学生对象："+student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}
